cmake_minimum_required(VERSION 3.22 FATAL_ERROR)

project(pjt1 LANGUAGES C CXX)

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")

# 打印系统信息
# 操作系统
message(STATUS "Configuring on/for ${CMAKE_SYSTEM_NAME}")
# message(STATUS "Is the C++ compiler loaded? ${CMAKE_CXX_COMPILER_LOADED}")
# if(CMAKE_CXX_COMPILER_LOADED)
#     message(STATUS "The C++ compiler ID is: ${CMAKE_CXX_COMPILER_ID}")
#     message(STATUS "Is the C++ from GNU? ${CMAKE_COMPILER_IS_GNUCXX}")
#     message(STATUS "The C++ compiler version is: ${CMAKE_CXX_COMPILER_VERSION}")
# endif()
# message(STATUS "Is the C compiler loaded? ${CMAKE_C_COMPILER_LOADED}")
# if(CMAKE_C_COMPILER_LOADED)
#     message(STATUS "The C compiler ID is: ${CMAKE_C_COMPILER_ID}")
#     message(STATUS "Is the C from GNU? ${CMAKE_COMPILER_IS_GNUCC}")
#     message(STATUS "The C compiler version is: ${CMAKE_C_COMPILER_VERSION}")
# endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)

add_subdirectory(src/add)

#option和set区别，option只有on/off
# 命令行传参可以覆盖掉
# cmake -DUSE_MESSAGE_LIBRARY=ON
option(USE_MESSAGE_LIBRARY "Compile message file into a library" OFF)
#set的值命令行传参传不进来
# set(USE_MESSAGE_LIBRARY OFF)
message(STATUS "Compile message file into a library? ${USE_MESSAGE_LIBRARY}")

#默认生成静态库
set(BUILD_SHARED_LIBS OFF)
#指定编译器
# set(CMAKE_CXX_COMPILER clang++)

# 引入变量包含message.hpp message.cpp
list(APPEND message_sources src/message.hpp src/message.cpp)
# 遍历list，为源文件文件设置编译属性
foreach(_source IN LISTS message_sources)
    set_source_files_properties(${_source} PROPERTIES COMPILE_FLAGS -O2)
  message(STATUS "Appending -O2 flag for ${_source}")
endforeach()
message(STATUS "Querying sources properties using plain syntax:")
foreach(_source ${sources_with_lower_optimization})
  get_source_file_property(_flags ${_source} COMPILE_FLAGS)
  message(STATUS "Source ${_source} has the following extra COMPILE_FLAGS: ${_flags}")
endforeach()

if(USE_MESSAGE_LIBRARY)
    add_library(message
        STATIC
        ${message_sources}
    )
    #设置编译属性 注意作用阈
    target_compile_options(message
    PRIVATE
    "-Wall"
    )
    add_executable(pjt1 src/main.cpp)
    target_link_libraries(pjt1 add message)
else(USE_MESSAGE_LIBRARY)
    add_executable(pjt1 src/main.cpp ${message_sources})
    target_link_libraries(pjt1 add)
endif(USE_MESSAGE_LIBRARY)
# 设置目标属性
set_target_properties(pjt1
    PROPERTIES
    CXX_STANDARD 17
    CXX_EXTENSIONS OFF
    CXX_STANDARD_REQUIRED ON
  )